package com.group.three.controller.orderController;

import com.github.pagehelper.PageInfo;
import com.group.three.entity.myOrder.*;
import com.group.three.service.myorderservice.AfterSaleReasonService;
import com.group.three.untils.SnowFlakeUtil;
import com.group.three.vo.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 买家端,售后管理接口
 */
@RestController
@Api(tags = "买家端,售后管理接口")
@RequestMapping("/buyer/order/afterSale")
public class AfterSaleBuyerController {
    @Autowired
    private AfterSaleReasonService afterSaleReasonService;

    @ApiOperation(value = "获取售后原因")
    @ApiImplicitParam(name = "serviceType", value = "售后类型", required = true, paramType = "path", allowableValues = "CANCEL,RETURN_GOODS,RETURN_MONEY,COMPLAIN")
    @GetMapping(value = "/get/afterSaleReason/{serviceType}")
    public JsonResult getAfterSaleReason(@PathVariable String serviceType) {
        List<AfterSaleReason> afterSaleReasons = afterSaleReasonService.selectAfterSaleReasons(serviceType);
        if (afterSaleReasons.size() != 0) {
            return new JsonResult("200", "查询售后原因成功", afterSaleReasons);
        }
        return new JsonResult("400", "查询售后原因失败");
    }

    @ApiOperation(value = "分页获取我的售后服务")
    @GetMapping(value = "/page")
    public JsonResult getByPage(AfterSaleSearchParams searchParams) {
        int pageNumber = searchParams.getPageNumber();
        int pageSize = searchParams.getPageSize();
        String memberId = searchParams.getMemberId();
        String keywords = searchParams.getKeywords();
        List<AfterSaleSearchParams> AfterSaleItem = afterSaleReasonService.selectAfterSale(memberId, keywords, pageNumber, pageSize);
        PageInfo<AfterSaleSearchParams> pageInfo = new PageInfo<>(AfterSaleItem);
        long total = pageInfo.getTotal();
        Map<String, Object> buyerOrder = new HashMap<>();
        buyerOrder.put("records", AfterSaleItem);
        buyerOrder.put("total", total);
        if (AfterSaleItem.size() != 0) {
            return new JsonResult("200", "售后数据查询成功", buyerOrder);
        }
        return new JsonResult("400", "没有数据");
    }

    @ApiOperation(value = "售后，取消售后")
    @PostMapping(value = "/cancel/{afterSaleSn}")
    public JsonResult cancelAfterSale(@PathVariable("afterSaleSn") String afterSaleSn) {
        int i = afterSaleReasonService.cancelAfterSale(afterSaleSn);
        if (i == 1) {
            return new JsonResult("200", "售后数据取消成功");
        }
        return new JsonResult("400", "售后数据取消失败");
    }

    @ApiOperation(value = "查看售后服务详情")
    @ApiImplicitParam(name = "sn", value = "售后单号", required = true, paramType = "path")
    @GetMapping(value = "/get/{sn}")
    public JsonResult get(@NotNull(message = "售后单号") @PathVariable("sn") String sn) {
        AfterSaleSearchParams afterSaleItem = afterSaleReasonService.AfterSaleItem(sn);
        if (afterSaleItem != null) {
            return new JsonResult("200", "售后详情数据查询成功", afterSaleItem);
        }
        return new JsonResult("400", "售后详情数据查询失败");
    }

    @ApiOperation(value = "获取售后日志")
    @ApiImplicitParam(name = "sn", value = "售后编号", required = true, paramType = "path")
    @GetMapping(value = "/get/getAfterSaleLog/{sn}")
    public JsonResult getAfterSaleLog(@PathVariable String sn) {
        List<AfterSaleLog> afterSaleLog = afterSaleReasonService.getAfterSaleLog(sn);
        if (afterSaleLog.size() != 0) {
            return new JsonResult("200", "售后日志数据查询成功", afterSaleLog);
        }
        return new JsonResult("400", "售后日志数据查询失败");
    }

    @ApiOperation(value = "获取申请售后页面信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "订单货物编号", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping(value = "/applyAfterSaleInfo/{sn}")
    //订单号
    public JsonResult applyAfterSaleInfo(@PathVariable String sn) {
        AfterSaleApplyVO AfterSaleItem=afterSaleReasonService.getAfterSaleInfo(sn);
        if (AfterSaleItem!= null) {
            return new JsonResult("200", "获取申请售后页面信息成功", AfterSaleItem);
        }
        return new JsonResult("400", "获取申请售后页面信息失败");
    }

    @PostMapping(value = "/save")
    @ApiImplicitParam(name = "orderItemSn", value = "订单货物编号", required = true, paramType = "query")
    @ApiOperation(value = "申请售后")
    public JsonResult save(AfterSaleDTO afterSaleDTO) {
        String s = String.valueOf(SnowFlakeUtil.getSnowFlakeId());
        String substring = s.substring(0, s.length() - 3);
        afterSaleDTO.setId(substring);
        afterSaleDTO.setSn("A"+substring);
        int i=afterSaleReasonService.saveAfterSale(afterSaleDTO);
        if (i== 1) {
            return new JsonResult("200", "申请售后成功");
        }
        return new JsonResult("400", "申请售后失败");

    }
}
